package co.yixiang.yshop.module.tables.controller.admin.table;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.tables.controller.admin.table.vo.*;
import co.yixiang.yshop.module.tables.dal.dataobject.table.TableInfoDO;
import co.yixiang.yshop.module.tables.convert.table.TableInfoConvert;
import co.yixiang.yshop.module.tables.service.table.TableInfoService;

@RestController
@Tag(name = "管理后台 - 餐桌")
@RequestMapping("/tables/table-info")
@Validated
public class TableInfoController {

    @Resource
    private TableInfoService tableInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建餐桌")
    @PreAuthorize("@ss.hasPermission('tables:table-info:create')")
    public CommonResult<Integer> createTableInfo(@Valid @RequestBody TableInfoCreateReqVO createReqVO) {
        return success(tableInfoService.createTableInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新餐桌")
    @PreAuthorize("@ss.hasPermission('tables:table-info:update')")
    public CommonResult<Boolean> updateTableInfo(@Valid @RequestBody TableInfoUpdateReqVO updateReqVO) {
        tableInfoService.updateTableInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除餐桌")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tables:table-info:delete')")
    public CommonResult<Boolean> deleteTableInfo(@RequestParam("id") Integer id) {
        tableInfoService.deleteTableInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得餐桌")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tables:table-info:query')")
    public CommonResult<TableInfoRespVO> getTableInfo(@RequestParam("id") Integer id) {
        TableInfoDO tableInfo = tableInfoService.getTableInfo(id);
        return success(TableInfoConvert.INSTANCE.convert(tableInfo));
    }

    @GetMapping("/list")
    @Operation(summary = "获得餐桌列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('tables:table-info:query')")
    public CommonResult<List<TableInfoRespVO>> getTableInfoList(@RequestParam("ids") Collection<Integer> ids) {
        List<TableInfoDO> list = tableInfoService.getTableInfoList(ids);
        return success(TableInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得餐桌分页")
    @PreAuthorize("@ss.hasPermission('tables:table-info:query')")
    public CommonResult<PageResult<TableInfoRespVO>> getTableInfoPage(@Valid TableInfoPageReqVO pageVO) {
        PageResult<TableInfoDO> pageResult = tableInfoService.getTableInfoPage(pageVO);
        return success(TableInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出餐桌 Excel")
    @PreAuthorize("@ss.hasPermission('tables:table-info:export')")
    @OperateLog(type = EXPORT)
    public void exportTableInfoExcel(@Valid TableInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TableInfoDO> list = tableInfoService.getTableInfoList(exportReqVO);
        // 导出 Excel
        List<TableInfoExcelVO> datas = TableInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "餐桌.xls", "数据", TableInfoExcelVO.class, datas);
    }

}
